package com.qumitech.visualnovel.service.poundegg.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.datasource.console.EggPoundDOMapper;
import com.qumitech.visualnovel.datasource.app.UserPoundEggStatisticsDOMapper;
import com.qumitech.visualnovel.model.dao.console.EggPoundDO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.poundegg.EggPoundQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.poundegg.UserPoundStatisticAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.poundegg.UserPoundStatisticDTO;
import com.qumitech.visualnovel.service.poundegg.PoundEggService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PoundEggServiceImpl implements PoundEggService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final String PLAYER_LIST_URL = "/account/admin/information";

    private static final String POUND_EGG_STATISTIC = "/poundegg/console/statistic/list";



    @Autowired
    EggPoundDOMapper eggPoundDOMapper;

    @Autowired
    UserPoundEggStatisticsDOMapper userPoundEggStatisticsDOMapper;

    @Override
    public void exportPoundEgg(EggPoundQueryDTO queryDTO, HttpServletResponse response) {

    }

    @Override
    public Integer countEggPound(EggPoundQueryDTO queryDTO) {
        return eggPoundDOMapper.countEggPound(queryDTO);
    }

    @Override
    public List<EggPoundDO> listEggPound(EggPoundQueryDTO queryDTO) {
        JSONArray accountIdArray = new JSONArray();
        List<EggPoundDO>  eggPoundDOList = eggPoundDOMapper.listEggPound(queryDTO);
        for(EggPoundDO eggPoundDO: eggPoundDOList){
            accountIdArray.add(eggPoundDO.getAccountId());
        }

        JSONObject postData = new JSONObject();
        postData.put("list_uid", accountIdArray);
        logger.info("postData:" + JSONObject.toJSONString(postData));
        String body = HttpInvokeUtil.httpPostJson(UrlConstant.SERVER_URL + PLAYER_LIST_URL, JSONObject.toJSONString(postData),UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        logger.info("body:" + rsp.toString());
        JSONObject data = (JSONObject)rsp.getData();
        JSONArray accountList = data.getJSONArray("account_list");
        Map<String, String> uidNicknameMap = new HashMap<>();
        for(int i=0;i<accountList.size();i++){
            JSONObject accountInfo = accountList.getJSONObject(i);
            uidNicknameMap.put(accountInfo.getString("uid"), accountInfo.getString("user_name"));
        }
        for(EggPoundDO eggPoundDO: eggPoundDOList){
            eggPoundDO.setUsername(uidNicknameMap.getOrDefault(eggPoundDO.getAccountId().toString(), ""));
        }
        return eggPoundDOList;
    }

    @Override
    public List<UserPoundStatisticDTO> listPoundEggStatistic(UserPoundStatisticAdminQueryDTO queryDTO) {
        return userPoundEggStatisticsDOMapper.listPoundEggStatistic(queryDTO);
    }

    @Override
    public Integer countPoundEggStatistic(UserPoundStatisticAdminQueryDTO queryDTO) {
        return userPoundEggStatisticsDOMapper.countPoundEggStatistic(queryDTO);
    }
}
